
#ifndef SNAKE_H
#define SNAKE_H


#include <QRectF>
#include <QPainter>
#include <QTimer>
#include <QObject>
#include <QKeyEvent>
#include <block.h>


class Snake: public QObject
{
    Q_OBJECT

public:
    explicit Snake(int x=0, int y=0, QObject *parent = nullptr);

    typedef enum Direct{Left=0,Right,Up,Down} RETDIRECT;
    void setloc(int x, int y);
    void paintsnake( QPainter & painter);
    void move();
    bool setdirect(RETDIRECT d);
    bool checkheadcrash(QRectF* rec);//头部的碰撞检测
    RETDIRECT dir();
    void start();
    void start(int);
    void stop();
    void changedirect(QKeyEvent *ev);
    void setactivetime(int);

    void setmovespeed(int);

    void longer(bool pd = false);

    QRectF realrect(int ind);

    void addblock(Block *blk);
    void addboudary(int w, int h);

private:
    QTimer timer;
    int gox[4]={-1, 1, 0, 0};
    int goy[4]={0,0, -1, 1};
    QList<QRectF> snakebody;

    int snakeBlockWidth;
    int snakeBlockHeight;
    qreal movespeed; //移动速度
    int activetime;
    RETDIRECT movedirect;
    qreal movelocx, movelocy;
    qreal locx, locy;
    void updatebodysize(int newwidth, int newheight);
    void allupdate();
    bool ISADD=0;
    void checkfail();
    qreal rightmax, buttommax;

signals:
    void check();
    void fail();
};

#endif // SNAKE_H
